<!--
 * @Descripttion:
 * @Author: Silence
 * @Date: 2023-05-30 19:47:31
 * @LastEditors: Silence
 * @LastEditTime: 2023-05-30 20:20:50
-->
<template>
  <div>
    <div class="box" style="position: fixed; top: 0; right: 0; z-index: 111">
      loading{{ loading }}===finished{{ finished }}==refreshing{{ refreshing }}
    </div>
    <van-pull-refresh v-model="refreshing" @refresh="onRefresh">
      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        @load="onLoad"
      >
        <van-cell
          v-for="(item, index) in list"
          :key="item.goods_name"
          :title="index + 1 + '====' + item.goods_name"
        />
      </van-list>
    </van-pull-refresh>
  </div>
</template>
<script>
import axios from 'axios'
export default {
  data () {
    return {
      pagenum: 0,
      pagesize: 20,
      list: [],
      loading: false,
      finished: false,
      refreshing: false
    }
  },
  created () {
    // this.getData()
  },
  methods: {
    onLoad () {
      // 异步更新数据
      // setTimeout 仅做示例，真实场景中一般为 ajax 请求
      if (this.pagenum > 3) {
        this.finished = true// 当达到设置条件时,将加载完成finished设置为true
        return// 并直接结束函数
      }
      this.pagenum++// 每次上拉加载时进行页数加一
      this.getData()// 进行上拉加载的时候,调用请求
    },
    async getData () {
      const res = await axios({
        url: 'http://vue-shop-api-t.itheima.net/api/private/v1/goods',
        params: {
          pagenum: this.pagenum,
          pagesize: this.pagesize
        },
        headers: {
          Authorization: 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjUwMCwicmlkIjowLCJpYXQiOjE2ODU0MzEyOTgsImV4cCI6MTY4NTUxNzY5OH0.rMLF5nxywTFwqBVP7RWyNJ_qEXoleuetLMbAHDz-NvI'
        }
      })
      console.log(res, 111)
      this.list = this.refreshing ? res.data.data.goods : [...this.list, ...res.data.data.goods]// 数据累加
      this.loading = false // 请求完成之后将loading设置为false就不会再进行加载
      this.refreshing = false // 下拉加载完成后将refreshing设置为true
    },
    onRefresh () {
      // 清空列表数据
      this.finished = false

      // 重新加载数据
      // 将 loading 设置为 true，表示处于加载状态
      this.pagenum = 0
      this.loading = true
      this.onLoad()
    }
  }
}
</script>
<style lang='less'  scoped>
</style>
